package com.c.ctgproject.ui.collect

import androidx.recyclerview.widget.LinearLayoutManager
import com.c.ctgproject.adapter.guang.MyCheckListAdapter
import com.c.ctgproject.adapter.guang.MyMaterialOutAdapter
import com.c.ctgproject.base.BaseActivityV2
import com.c.ctgproject.databinding.ActivityMaterialMyCreateOutBinding
import com.c.ctgproject.entity.guang.OutCheckEntity
import com.c.ctgproject.http.ApiMethods
import com.c.ctgproject.http.MyObserverHandlerLoading
import com.c.ctgproject.utils.*

/**
 *作者:LiBW
 *创建日期: 2021/10/15
 *描述: 我创建的出库计划 页面
 */
class MaterialMyCreateOutActivity : BaseActivityV2(), MyCheckListAdapter.MyCheckListAdapterListener, MyMaterialOutAdapter.MyMaterialOutAdapterListener {
    private val mBinding by lazy { ActivityMaterialMyCreateOutBinding.inflate(layoutInflater) }
    override fun getLayout() = mBinding.root
    private var page = 1
    private val mList = ArrayList<OutCheckEntity.DataBean.ListBean>()
    private val mAdapter by lazy { MyMaterialOutAdapter(this, mList, this) }
    override fun event() {
        getData()
        mBinding.mRecyclerview.adapter = mAdapter
        mBinding.mRecyclerview.layoutManager = LinearLayoutManager(this)
    }

    private var search: String = ""


    override fun click() {
        mBinding.mSearchET.handlerText {
            if (it.isEmpty() || it == "") {
                search = ""
                page = 1
                getData()
            }
        }
        mBinding.mSearchTV.setOnClickListener {
            search = mBinding.mSearchET.text.handlerNull()
            page = 1
            getData()
        }

        mBinding.mTabLayout.getTabAt(1)?.select()
        mBinding.mTabLayout.handlerSelectReturnText {
            when (it) {
                "草稿单" -> {
                    status = 0
                    page = 1
                    getData()
                }
                "待审批" -> {
                    page = 1
                    status = 1
                    getData()
                }
                "已审批" -> {
                    page = 1
                    status = 2
                    getData()
                }
            }
        }
        mBinding.mRefresh.setOnLoadMoreListener {
            page++
            getData()
        }

        mBinding.mRefresh.setOnRefreshListener {
            page = 1
            getData()
        }
    }

    /**放行条状态 0-新建(草稿) 1-待审批 2-已审批*/
    private var status = 1

    private fun getData() {
        ApiMethods.apiSubscribe(ApiMethods.retrofit.outCheckList(1, search, page, status), MyObserverHandlerLoading(this, loading, mBinding.mRefresh) {
            MyLog.d("出库审批列表:$it")
            val data = getGson().fromJson(it, OutCheckEntity::class.java).data
            data?.list?.let { list ->
                if (page == 1) mList.clear()
                mList.addAll(list)
                mAdapter.notifyDataSetChanged()
                mBinding.mRecyclerview.handlerPosition(page)
            }
        })
    }


    override fun onItemDeleteClick(position: Int) {
        val id = mList[position].outOrderId
        id?.let {
            commonDialog("确定要删除该草稿单么") {
//                ApiMethods.apiSubscribe(ApiMethods.retrofit.deleteMyDraft(id), MyObserverHandlerLoading(this, loading, mBinding.mRefresh) {
//                    MyLog.d("回调:$it")
//                    showToast("删除成功")
//                    mList.removeAt(position)
//                    mAdapter.notifyItemRemoved(position)
//                })
            }
        }

    }

    override fun onItemLongClick(position: Int) {

    }
}